package com.sgl.gk.service.impl;

import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.utils.DateUtils;
import com.sgl.common.utils.StringUtils;
import com.sgl.gk.controller.vo.*;
import com.sgl.gk.domain.*;
import com.sgl.gk.mapper.*;
import com.sgl.gk.service.GkStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author:xec
 * @date:2022/11/17 9:24
 **/
@Service
@Slf4j
public class GkStatisticsServiceImpl implements GkStatisticsService {

    @Autowired
    GkStatisticsMapper gkStatisticsMapper;

    @Autowired
    GkStationConfigurationConnectMapper gkStationConfigurationConnectMapper;

    @Autowired
    GkStationConfigurationValuesMapper gkStationConfigurationValuesMapper;

    @Autowired
    GkSettingMapper gkSettingMapper;

    @Autowired
    GkShowStationInfoMapper gkShowStationInfoMapper;

    @Autowired
    GkReservedInfoMapper gkReservedInfoMapper;

    @Autowired
    GkHourCompleteMapper gkHourCompleteMapper;

    @Override
    public AjaxResult dataStatisticsInfos() {
        List<GkStatistics> gkStatistics = null;
        try {
            gkStatistics = gkStatisticsMapper.toDayInfo();
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        GkStatisticsVo gkStatisticsVo = new GkStatisticsVo();
        for (GkStatistics gkStatistic : gkStatistics) {
            if (gkStatistic.getGkSignalId().longValue()==24) {
                gkStatisticsVo.setId(gkStatistic.getId());
                gkStatisticsVo.setPlanProductNum(gkStatistic.getPlanProductNum());
                gkStatisticsVo.setProductNum(gkStatistic.getProductNum());
                gkStatisticsVo.setProductNgNum(gkStatistic.getProductNgNum());
                gkStatisticsVo.setCreateAt(gkStatistic.getCreateAt());
                gkStatisticsVo.setPlanBit(gkStatistic.getPlanBit());
                float realBit = gkStatistic.getRealBit() / 1000;
                gkStatisticsVo.setRealBit(realBit);
                gkStatisticsVo.setRuntime(gkStatistic.getRuntime()/60);
                gkStatisticsVo.setLastModifyAt(gkStatistic.getLastModifyAt());
                gkStatisticsVo.setActualProductionQuantity(gkStatistic.getProductRealNum());
            }
        }
        return AjaxResult.success(gkStatisticsVo);
    }



    @Override
    public AjaxResult dailyPlanned() {
        List<GkSetting> list = gkSettingMapper.list();
        return AjaxResult.success(list);
    }

    @Transactional
    @Override
    public AjaxResult updateDailyPlanned(GkSetting gkSetting) {
        List<GkStatistics> gkStatistics = gkStatisticsMapper.toDayInfo();
        if (gkSetting.getPlanProductNum()!=null) {
            for (GkStatistics gkStatistic : gkStatistics) {
                GkStatistics gkStatistics1 = new GkStatistics();
                gkStatistics1.setId(gkStatistic.getId());
                gkStatistics1.setPlanProductNum(gkSetting.getPlanProductNum());
                gkStatisticsMapper.edit(gkStatistics1);
            }
        }
        if (gkSetting.getPlanBit()!=null){
            for (GkStatistics gkStatistic : gkStatistics) {
                GkStatistics gkStatistics1 = new GkStatistics();
                gkStatistics1.setId(gkStatistic.getId());
                gkStatistics1.setPlanBit(gkSetting.getPlanBit());
                gkStatisticsMapper.edit(gkStatistics1);
            }
        }
        int a = gkSettingMapper.edit(gkSetting);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult stationData() {
        List<GkShowStationInfo> gkShowStationInfos = gkShowStationInfoMapper.selectAllInfo();
        gkShowStationInfos = gkShowStationInfos.stream().filter(gkShow->gkShow.getIsShow()!=0).collect(Collectors.toList());
        List<GkShowStationInfoVo> gkShowStationInfoVoList = new ArrayList<>();
        for (GkShowStationInfo gkShowStationInfo : gkShowStationInfos) {
            GkShowStationInfoVo gkShowStationInfoVo = new GkShowStationInfoVo();
            BeanUtils.copyProperties(gkShowStationInfo,gkShowStationInfoVo,new String[]{"AllCount","OKRate"});
            if (gkShowStationInfoVo.getOkCount()==null){
                gkShowStationInfoVo.setOkCount(0);
            }
            if (gkShowStationInfoVo.getNgCount()==null){
                gkShowStationInfoVo.setNgCount(0);
            }
            gkShowStationInfoVo.setAllCount(gkShowStationInfo.getOkCount()+gkShowStationInfo.getNgCount());
            Integer rate = 0;
            if (gkShowStationInfoVo.getAllCount()!=0){
                rate = (int)(gkShowStationInfo.getOkCount().floatValue() / gkShowStationInfoVo.getAllCount() * 100);
            }
            gkShowStationInfoVo.setOKRate(rate+"%");
            Integer cycleTime = gkShowStationInfo.getCycleTime();
            float v = cycleTime.floatValue() / 1000 ;
            int i = (int) (v * 100);
            Double v1 = (i / 100.0);
            gkShowStationInfoVo.setCycleTime(v1.floatValue());
            gkShowStationInfoVoList.add(gkShowStationInfoVo);
        }
        return AjaxResult.success(gkShowStationInfoVoList);
    }

    @Override
    public AjaxResult editStationInfo(List<GkShowStationInfo> gkShowStationInfoList) {
        for (GkShowStationInfo gkShowStationInfo : gkShowStationInfoList) {
            if (gkShowStationInfo.getIsShow()==1) {
                if (gkShowStationInfo.getShowStationPlanNum() == null) {
                    return AjaxResult.error("显示的工位必须传入计划产量");
                }
            }
        }
        int row = gkShowStationInfoMapper.editList(gkShowStationInfoList);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult stationAllInfo() {
        return AjaxResult.success(gkShowStationInfoMapper.selectAllInfo());
    }

    @Override
    public AjaxResult getOKAndCompleteRate() {
        List<GkShowStationInfo> gkShowStationInfos = gkShowStationInfoMapper.selectAllInfo();
        gkShowStationInfos = gkShowStationInfos.stream().filter(gkShow->gkShow.getIsShow()!=0).collect(Collectors.toList());
        List<String> names = new ArrayList<>();
        List<Float> OkRates = new ArrayList<>();
        List<Float> CompleteRates = new ArrayList<>();
        for (GkShowStationInfo gkShowStationInfo : gkShowStationInfos) {
            names.add(gkShowStationInfo.getShowStationName());
            Integer okCount = gkShowStationInfo.getOkCount();
            Integer ngCount = gkShowStationInfo.getNgCount();

            float v = okCount.floatValue() + ngCount.floatValue();
            if (v!=0) {
                Float okRate = okCount.floatValue() / v;
                okRate = ((float) (((int) (okRate * 10000)) / 100.0));
                OkRates.add(okRate);
            }else{
                OkRates.add(0f);
            }
            if (gkShowStationInfo.getShowStationPlanNum()!=null&&gkShowStationInfo.getShowStationPlanNum()!=0) {
                Float completeRate = v / gkShowStationInfo.getShowStationPlanNum().floatValue();
                completeRate = ((float) (((int) (completeRate * 10000)) / 100.0));
                CompleteRates.add(completeRate);
            }else {
                CompleteRates.add(0f);
            }
        }
        List<StationOKAndCompleteRateVo> stationOKAndCompleteRateVos = new ArrayList<>();
        StationOKAndCompleteRateVo stationOKAndCompleteRateVo = new StationOKAndCompleteRateVo();
        stationOKAndCompleteRateVo.setName("合格率");
        stationOKAndCompleteRateVo.setData(OkRates);
        stationOKAndCompleteRateVos.add(stationOKAndCompleteRateVo);
        StationOKAndCompleteRateVo stationOKAndCompleteRateVo1 = new StationOKAndCompleteRateVo();
        stationOKAndCompleteRateVo1.setName("完成率");
        stationOKAndCompleteRateVo1.setData(CompleteRates);
        stationOKAndCompleteRateVos.add(stationOKAndCompleteRateVo1);
        Map<String,Object> map = new HashMap<>();
        map.put("stationOkAndCompleteRates",stationOKAndCompleteRateVos);
        map.put("names",names);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getCompleteCntByTime() {
        List<GkHourComplete> gkHourCompletes = gkHourCompleteMapper.selectInfoByToDay();
        List<GkHourCompleteVo> gkHourCompleteVos = new ArrayList<>();
        for (Integer i = 0; i < 24; i++) {
            String min = i<10?"0"+i:i.toString();
            Integer i1 = i + 1;
            String endMin = i+1<10?"0"+(i+1):i1.toString();
            GkHourCompleteVo gkHourCompleteVo = new GkHourCompleteVo();
            gkHourCompleteVo.setCompleteNum(0);
            gkHourCompleteVo.setDate(min+":00-"+endMin+":00");
            gkHourCompleteVos.add(gkHourCompleteVo);
        }
        for (GkHourCompleteVo gkHourCompleteVo : gkHourCompleteVos) {
            for (GkHourComplete gkHourComplete : gkHourCompletes) {
                Date end = gkHourComplete.getCreateAt();
                Date start = DateUtils.addHours(end, -1);
                SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                String endStr = format.format(end);
                String startStr = format.format(start);
                String date = startStr + "-" + endStr;
                if (date.equals(gkHourCompleteVo.getDate())) {
                    gkHourCompleteVo.setDate(date);
                    gkHourCompleteVo.setId(gkHourComplete.getId());
                    gkHourCompleteVo.setCompleteNum(gkHourComplete.getCompleteNum());
                }
            }
        }
        return AjaxResult.success(gkHourCompleteVos);
    }

    @Override
    public AjaxResult productionLineInfo() {
        List<GkReservedInfo> gkReservedInfoList = gkReservedInfoMapper.selectInfoToUse();
        return AjaxResult.success(gkReservedInfoList);
    }

    @Override
    public AjaxResult productionLineAllInfo() {
        List<GkReservedInfo> gkReservedInfoList = gkReservedInfoMapper.selectAllInfo();
        return AjaxResult.success(gkReservedInfoList);
    }

    @Override
    public AjaxResult editProductionLineInfo(List<GkReservedInfo> gkReservedInfoList) {
        for (GkReservedInfo gkReservedInfo : gkReservedInfoList) {
            if (StringUtils.isEmpty(gkReservedInfo.getValue())||StringUtils.isEmpty(gkReservedInfo.getKey())){
                return AjaxResult.error("键和值不能为空");
            }
        }
        int row = gkReservedInfoMapper.editList(gkReservedInfoList);
        return AjaxResult.success();
    }


}
